﻿using MES.Server.Entities;
using MES.Server.EntitiesDTO;
using System.Security.Claims;

namespace MES.Server.AuthServer.Utility.JwtService
{
    /// <summary>
    /// 颁发Ttoken的抽象类
    /// </summary>
    public abstract class CustomJWTService
    {
        /// <summary>
        /// 抽象函数，生成Token的，具体实现，留给实现类来实现---多种方式来实现
        /// </summary>
        /// <param name="user">SysUserInfo，从数据库中查询出来以后</param>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public abstract string CreateToken(UserEntity user, out string refreshToken);

        /// <summary>
        /// 生成token 准备用户的信息，写入到Claim
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        protected virtual List<Claim> UserToClaim(UserEntity user)
        {
            //准备有效载荷
            List<Claim> claimsArray = new List<Claim>()
            {
               //new Claim(ClaimTypes.Sid, user.UserId.ToString()),
               new Claim(ClaimTypes.Name, user.UserName?? string.Empty),
               //new Claim(ClaimTypes.MobilePhone, user.Mobile?? string.Empty),
               //new Claim(ClaimTypes.OtherPhone, user.Phone?? string.Empty),
               //new Claim(ClaimTypes.StreetAddress, user.Address?? string.Empty),
               //new Claim(ClaimTypes.Email, user.Email?? string.Empty),
               //new Claim("QQ", user.QQ.ToString()),
               //new Claim("WeChat", user.WeChat?? string.Empty),
               //new Claim("Sex", user.Sex.ToString())
            };
            //foreach (var roleId in user.RoleIdList!)
            //{
            //    claimsArray.Add(new Claim(ClaimTypes.Role, roleId.ToString()));
            //}
            //foreach (var menu in user.UserMenuList!)
            //{
            //    claimsArray.Add(new Claim("Menus", Newtonsoft.Json.JsonConvert.SerializeObject(menu)));
            //}
            //foreach (var btn in user.UserBtnList!)
            //{
            //    claimsArray.Add(new Claim("Btns", btn.Value));
            //}
            return claimsArray;
        }
    }
}
